using System.Linq;
using System.Threading.Tasks;
using iSlideTools.LocalServer.Data;
using iSlideTools.LocalServer.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace iSlideTools.LocalServer.Controllers
{
    [Authorize]
    public class ValidMacAddressesController : Controller
    {
        private readonly DataContext _context;

        public ValidMacAddressesController(DataContext context)
        {
            _context = context;
        }

        // GET: ValidMacAddresses
        public async Task<IActionResult> Index()
        {
            return View(await _context.ValidMacAddresses.ToListAsync());
        }

        // GET: ValidMacAddresses/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
                return NotFound();

            var validMacAddress = await _context.ValidMacAddresses
                .SingleOrDefaultAsync(m => m.Id == id);
            if (validMacAddress == null)
                return NotFound();

            return View(validMacAddress);
        }

        // GET: ValidMacAddresses/Create
        [Authorize(Policy = AuthorizationInfo.AdministratorsPolicy)]
        public IActionResult Create()
        {
            return View();
        }

        // POST: ValidMacAddresses/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Policy = AuthorizationInfo.AdministratorsPolicy)]
        public async Task<IActionResult> Create(
            [Bind("Id,UserName,MacAddress,IsEnabled,Comments")] ValidMacAddress validMacAddress)
        {
            if (ModelState.IsValid)
            {
                _context.Add(validMacAddress);
                await _context.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            return View(validMacAddress);
        }

        // GET: ValidMacAddresses/Edit/5
        [Authorize(Policy = AuthorizationInfo.AdministratorsPolicy)]
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
                return NotFound();

            var validMacAddress = await _context.ValidMacAddresses.SingleOrDefaultAsync(m => m.Id == id);
            if (validMacAddress == null)
                return NotFound();
            return View(validMacAddress);
        }

        // POST: ValidMacAddresses/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Policy = AuthorizationInfo.AdministratorsPolicy)]
        public async Task<IActionResult> Edit(int id,
            [Bind("Id,UserName,MacAddress,IsEnabled,Comments")] ValidMacAddress validMacAddress)
        {
            if (id != validMacAddress.Id)
                return NotFound();

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(validMacAddress);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ValidMacAddressExists(validMacAddress.Id))
                        return NotFound();
                    throw;
                }
                return RedirectToAction("Index");
            }
            return View(validMacAddress);
        }

        // GET: ValidMacAddresses/Delete/5
        [Authorize(Policy = AuthorizationInfo.AdministratorsPolicy)]
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null)
                return NotFound();

            var validMacAddress = await _context.ValidMacAddresses
                .SingleOrDefaultAsync(m => m.Id == id);
            if (validMacAddress == null)
                return NotFound();

            return View(validMacAddress);
        }

        // POST: ValidMacAddresses/Delete/5
        [HttpPost]
        [ActionName("Delete")]
        [ValidateAntiForgeryToken]
        [Authorize(Policy = AuthorizationInfo.AdministratorsPolicy)]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            var validMacAddress = await _context.ValidMacAddresses.SingleOrDefaultAsync(m => m.Id == id);
            _context.ValidMacAddresses.Remove(validMacAddress);
            await _context.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        private bool ValidMacAddressExists(int id)
        {
            return _context.ValidMacAddresses.Any(e => e.Id == id);
        }
    }
}